package cn.flying.base.process.service;//package cn.flying.cloud.activiti.service;
//
//import javax.annotation.Resource;
//
//import java.io.InputStream;
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import org.activiti.bpmn.model.BpmnModel;
//import org.activiti.bpmn.model.FlowElement;
//import org.activiti.bpmn.model.FlowNode;
//import org.activiti.bpmn.model.Process;
//import org.activiti.bpmn.model.SubProcess;
//import org.activiti.engine.HistoryService;
//import org.activiti.engine.ProcessEngine;
//import org.activiti.engine.ProcessEngineConfiguration;
//import org.activiti.engine.RepositoryService;
//import org.activiti.engine.history.HistoricActivityInstance;
//import org.activiti.engine.history.HistoricProcessInstance;
//import org.activiti.engine.repository.ProcessDefinition;
//import org.activiti.engine.repository.ProcessDefinitionQuery;
//import org.activiti.image.ProcessDiagramGenerator;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Service;
//
//import cn.flying.cloud.activiti.mapper.ActNativeQueryCommonMapper;
//import cn.flying.cloud.common.utils.StringUtils;
//
///**
// * @description: 流程图
// * @author: lvyong
// * @date: 2023年02月16日 18:03
// * @version: 1.0
// */
//@Service
//public class ProcessDiagramService {
//    private Logger logger = LoggerFactory.getLogger(this.getClass());
//
//    @Resource
//    private RepositoryService repositoryService;
//    @Resource
//    private ProcessEngine processEngine;
//    @Resource
//    private HistoryService historyService;
//    @Resource
//    private ActNativeQueryCommonMapper actNativeQueryCommonMapper;
//
//    /**
//     * 画运行时流程图
//     * @param processDefId
//     * @param procInstId
//     * @param tenantId
//     * @return
//     */
//    public byte[] drawBpmnDiagram(String processDefId, String procInstId, String tenantId) {
//        String[] keys = processDefId.split(":");
//        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery().processDefinitionKey(keys[0]).processDefinitionVersion(Integer.parseInt(keys[1]));
//        if (StringUtils.isNotBlank(tenantId)) {
//            processDefinitionQuery.processDefinitionTenantId(tenantId);
//        }
//        ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
//        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
//        ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
//        ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
//        InputStream inputStream = null;
//        if (StringUtils.isBlank(procInstId)) {
//            inputStream = diagramGenerator.generateDiagram(bpmnModel, "PNG", new ArrayList<String>(), new ArrayList<String>(),
//                    processEngineConfiguration.getActivityFontName(),
//                    processEngineConfiguration.getLabelFontName(),
//                    processEngineConfiguration.getAnnotationFontName(),
//                    processEngineConfiguration.getClassLoader(),
//                    1.0);
//        } else {
//            inputStream = diagramGenerator.generateDiagram(bpmnModel, "PNG", new ArrayList<String>(), new ArrayList<String>(),
//                    processEngineConfiguration.getActivityFontName(),
//                    processEngineConfiguration.getLabelFontName(),
//                    processEngineConfiguration.getAnnotationFontName(),
//                    processEngineConfiguration.getClassLoader(),
//                    1.0);
//        }
//    }
//
//    /**
//     * 获取流程图的高亮节点
//     * @param procInstId
//     * @return
//     */
//    private Map<String, Object> getHighLighted(String procInstId) {
//        Map<String, Object> map = new HashMap<>();
//        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(procInstId).singleResult();
//        List<String> activeActivityIds = new ArrayList<>();
//        List<String> currentActivityIds = new ArrayList<>();
//        List<String> exceptionActivityIds = new ArrayList<>();
//        List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery().processInstanceId(procInstId).list();
//        for (HistoricActivityInstance hai : historicActivityInstances) {
//            activeActivityIds.add(hai.getActivityId());
//            if (hai.getEndTime() != null) {
//                currentActivityIds.add(hai.getActivityId());
//            }
//        }
//
//        if (currentActivityIds.size() == 0) {
//            exceptionActivityIds = actNativeQueryCommonMapper.getExceptionActivityIds(procInstId);
//            activeActivityIds.addAll(exceptionActivityIds);
//            currentActivityIds.addAll(exceptionActivityIds);
//        }
//
//        BpmnModel bpmnModel = repositoryService.getBpmnModel(historicProcessInstance.getProcessDefinitionId());
//        Process processById = bpmnModel.getProcessById(historicProcessInstance.getProcessDefinitionKey());
//        List<String> highLightFlows =  new ArrayList<>();
//
//    }
//
//
//    private void getHighLightFlows(Collection<FlowElement> flowElements, List<String> highLightFlows, List<String> historicActivityInstList) {
//        for (FlowElement flowElement : flowElements) {
//            //判断是否是子流程
//            if (flowElement instanceof SubProcess) {
//                getHighLightFlows(((SubProcess)flowElement).getFlowElements(), highLightFlows, historicActivityInstList);
//            }
//
//            boolean paralleGateway = false;
//            if (historicActivityInstList.contains(flowElement.getId())) {
//                List<HistoricActivityInstance> outGoingActivitys = new ArrayList<>();
//                FlowNode flowNode = (FlowNode) flowElement;
//
//            }
//        }
//    }
//
//}
